<?php
/**
 *
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018/8/9
 * Time: 上午11:15
 * Updatetime:
 * updater：
 */
namespace WAdmin\Controller;


class DoorController extends BaseController
{

    public function __check($start_time,$end_time,$car_no)
    {

        if ($start_time>$end_time) return showErrorMsg('开始时间不能比结束时间大',100002);

        $uid = $this->userInfo->id;

        if (!empty($car_no)){
            $carInfo = M('car')->where(['car_no'=>$car_no,'uid'=>$uid])->find();

            if (empty($carInfo)) return showErrorMsg(L("_DATA_NOT_EXIT_"),100002);
        }

        return true;
    }

    public function groupList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $model = M('car');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,1,'gid desc','group_concat(id) ids,gid','1000','gid');

        if (!empty($list)){
            foreach ($list as $k=>$v){
                if (empty($v['gid'])){
                    foreach (explode(',',$v['ids']) as $key=>$val){
                        $list[$k+$key] = $model->field('id,gid,headsmall,car_no')->where(['id'=>$val])->find();
                        $list[$k+$key]['_child'] = [];
                    }
                }else{
                    $list[$k]['id'] = '';

                    $list[$k]['gid'] = $v['gid'];//群的id


                    $list[$k]['headsmall'] = '';//群的图标
                    $list[$k]['car_no'] = M('car_group')->field('title')->where(['id'=>$v['gid']])->getField('title');

                    $list[$k]['_child'] = [];
                    foreach (explode(',',$v['ids']) as $key=>$val){
                        $list[$k]['_child'][$key] = $model->field('id,headsmall,car_no')->where(['id'=>$val])->find();
                        $list[$k]['_child'][$key]['_child'] = [];
                    }
                    unset($list[$k]['ids']);
                }
            }
        }
        return showList($list,new \stdClass());
    }

    /**
     * @desc 实时门禁报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function door($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car c');

        if (!empty($param['keyword'])){
            $map['c.car_no'] = array('like','%'.$param['keyword'].'%');
        }

        if (!empty($param['gid'])){
            $map['c.gid'] = $param['gid'];
        }

        if (!empty($param['id'])){
            $map['id'] = $param['id'];
        }
        $map['c.uid'] = $this->userInfo->id;
        $map['c.status'] = 3;

        $join = "left join think_car_attr a on c.id = a.car_id ";

        list($list,$page) = _listPage($model,$map,$p,null,"c.id,c.car_no,c.equipment_no,a.door,c.create_time",$listRows,null,$join);

        return showList($list,$page);
    }


    /**
     * @desc 门禁报表详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array|void
     */
    public function doorHistory($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_no,start_time,end_time');

        unset($check);

        $car_no = $param['car_no'];

        $start_time = strtotime($param['start_time']);
        $end_time   = strtotime($param['end_time']);

        $map['create_time'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        $ret= $model
            ->field("create_time x,door val")
            ->where($map)
            ->select();


        $weekarray=array("日","一","二","三","四","五","六");

        $date_array = [
            '0:00',
            '1:00',
            '2:00',
            '3:00',
            '4:00',
            '5:00',
            '6:00',
            '7:00',
            '8:00',
            '9:00',
            '10:00',
            '11:00',
            '12:00',
            '13:00',
            '14:00',
            '15:00',
            '16:00',
            '17:00',
            '18:00',
            '19:00',
            '20:00',
            '21:00',
            '22:00',
            '23:00',
            '0:00',
        ];

        foreach ($ret as $k=>$v){
            $ret[$k]['y'] = '星期'.$weekarray[date('w',strtotime($v['x']))];

            $ret[$k]['x_percent'] =  round(date('i',$v['x'])/60 * 100,2);

            $x = date('G',$v['x']);

            $key = array_search($x.':00',$date_array);

            $ret[$k]['x'] = $x.':00-'.$date_array[$key+1];

        }
        return showList($ret,new \stdClass());
    }
}